import axios from 'axios'
import { config } from '../config'
import qs from 'qs'

let CryptoJS = require('crypto-js');
const apiURL = config.baseApi;
const secretKey = config.secretKey;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
const httpRequest = (params, url) => {
    const timestamp = Math.round(new Date().getTime() / 1000)
    var encodeUrl = encodeURIComponent(`${url}`);
    var signature = CryptoJS.HmacSHA1(encodeUrl, secretKey).toString();

    params.data['secretKey'] = secretKey;
    params.data['signature'] = signature;
    params.data['timeStamp'] = timestamp;
    if (params.method == 'POST' || !params.method) {
        return axios.post(url, qs.stringify(params.data)).then(res => {
                return res
            })
            .catch(function(e) {
                console.log('网络错误,请重试')
            })
    }

    if (params.method == 'GET') {
        return axios.get(url, {
                params: params.data || {},
            }).then(res => {
                return res
            })
            .catch(function(e) {
                console.log('网络错误,请重试')
            })
    }
};

// get openid
export const getOpenId = (code) => {
        return httpRequest({ data: {}, method: 'POST' }, `${config.getOpenId}?code=${code}`);
    }
    //获取unionId
export const h5GetUnionId = code => {
    return httpRequest({ data: {}, method: 'POST' }, `${apiURL}/mindMain/h5GetUnionId?code=${code}`)
}

// 1.1 发送验证码
export const sendVerifyCode = (params) => {
    return httpRequest(params, `${apiURL}/user/sendVerifyCode`);
}

// 1.2 验证验证码
export const verificationUser = (params) => {
    return httpRequest(params, `${apiURL}/user/verificationUser_V3_2`);
}

// 1.3 注册
export const register = (params) => {
    return httpRequest(params, `${apiURL}/user/register_V3_2`);
}

// 1.4 用户账户登录
export const pwdLogin = (params) => {
    console.log(`${apiURL}/user/login`, 'url');
    return httpRequest(params, `${apiURL}/user/login`);
}

// 1.5 用户验证码登陆
export const codeLogin = (params) => {
    return httpRequest(params, `${apiURL}/user/phoneLogin`);
}

// 1.6 微信登陆
export const wechatLogin = (params) => {
    return httpRequest(params, `${apiURL}/user/weixinLogin`);
}

// 1.7 获取用户信息
export const getUserInfo = (params) => {
    return httpRequest(params, `${apiURL}/UserInfo/selectByUidDetail`);
}

// 1.8 修改用户信息
export const editUserInfo = (params) => {
    return httpRequest(params, `${apiURL}/resume/addOrUpdateUserInfo`);
}

// 1.9 上传头像
export const uploadAvatar = (params) => {
    return httpRequest(params, config.UpLoadImage);
}

// 2.0 忘记密码
export const forgetPassword = (params) => {
    return httpRequest(params, `${apiURL}/user/editPassword`);
}

// 2.1.25 查询会员
export const getVipInfo = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/cardNum_new`);
}

// 2.1.11 分类列表接口
export const getCategories = (params) => {
    console.log(`${apiURL}/mindCategory/getAllMindCategoryV3`, 'url');
    console.log(params);

    return httpRequest(params, `${apiURL}/mindCategory/getAllMindCategoryV3`);
}

// 2.1.2 分类下的书籍列表
export const getListByCategoryId = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/getCatetoryListByCodition`);
}

// 2.1.14 我的听书
export const getMyListenBooks = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/getListenBookVip`);
}

// 2.1.6 主题列表
export const getThemeList = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/selectThemeList`);
}

// 2.1.6 主题书单列表
export const getThemeBookList = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/selectThemeBooksList`);
}

// 猜你喜欢
export const likeBooks = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/guessYourLikeBook`);
}

// 获取首页列表
export const getHomeList = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/getEveryDayListenBookV3`);
};

// 2.1.3 书籍详情页
export const getBookDetail = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/getMindMainByIdV3`);
}

// 2.1.23 实体卡激活
export const cardActived = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/cardActived`);
}

// 2.1.24 第三方支付
export const wechatPay = (params) => {
    return httpRequest(params, `${apiURL}/weiChatMini/weChatOrder`);
}

// 2.1.13 买书
export const buyBook = (params) => {
    return httpRequest(params, `${apiURL}/mindBuy/takeABook`);
}

// 2.1.10 拿下
export const oneKeyTake = (params) => {
    return httpRequest(params, `${apiURL}/mindBuy/oneKeyTake`);
}

// 次卡买书
export const onceCardBuyBook = (params) => {
    return httpRequest(params, `${apiURL}/mindBuy/buyABook`);
}

// 扣除次卡
export const refreshOnceCard = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/useTimeCard_new`);
}

// 查询书被点赞次数
export const bookPraiseNumber = (params) => {
    return httpRequest(params, `${apiURL}/mindPraise/isPraise`);
}

// 点赞/取消点赞
export const bookPraise = (params) => {
    return httpRequest(params, `${apiURL}/mindPraise/dotLike`);
}

// 邀请人数
export const inviterNumber = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/friendNum`);
}

// 已买到的书
export const myBoughtBooks = (params) => {
    return httpRequest(params, `${apiURL}/mindBuy/getBuyMindList`);
}

// 用户个人数据
export const userProfit = (params) => {
    return httpRequest(params, `${apiURL}/UserInfo/selectByUidDetail`);
}

// 用户账户
export const userAccount = (params) => {
    return httpRequest(params, `${apiURL}/account/selectUserAcountBy`);
}

// 用户海报
export const userPoster = (params) => {
    return httpRequest(params, `${apiURL}/mindMain/getPoster`)
}

// 用户支付成功，更新vip
export const paySuccessedUpdateVip = (params) => {
        return httpRequest(params, `${apiURL}/mindBuy/vipByThirdPay`);
    }
    /**
     * 微信统一下单
     */
export const wxOrder = (params, url) => {
    return httpRequest(params, url);
}

// 懂行余额支付
export const balancePay = (params) => {
    return httpRequest(params, `${apiURL}/mindBuy/buyMindOrMemberV3_1`);
}

/**
 * 购买
 * @param {} params 
 */
export const buyBookPay = (params) => {
    return httpRequest(params, `${apiURL}/webViewDitails/buyBook`);
}

export const cardShareReward = params => {
    return httpRequest(params, `${apiURL}/mindMain/cardShareReward`);
}

// export default {
//   sendVerifyCode,
//   verificationUser,
//   register,
//   pwdLogin,
//   codeLogin,
//   wechatLogin,
//   getMyListenBooks,
//   getBookDetail,
//   getHomeList,
//   getCategories,
//   getListByCategoryId,
//   getThemeList,
//   getThemeBookList,
//   cardActived,
//   getUserInfo,
//   editUserInfo,
//   uploadAvatar,
//   forgetPassword,
//   getVipInfo,
//   wechatPay,
//   buyBook,
//   likeBooks,
//   inviterNumber,
//   myBoughtBooks,
//   userProfit,
//   onceCardBuyBook,
//   bookPraiseNumber,
//   bookPraise,
//   userAccount,
//   userPoster,
//   paySuccessedUpdateVip,
//   balancePay,
//   refreshOnceCard,
//   getOpenId
// };